package mars.ColorPiano;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class ShapePanel extends JPanel {
	
	public enum Shape {
		Square, Triangle, Circle, Hexagon, Pentagon
	}
	
	public static Color[] colors = {Color.red, Color.green, Color.blue, Color.yellow, Color.orange};
		
	private Shape mShape;
	private Color mColor;
	private Color mBackgroundColor;
	
	private static final long serialVersionUID = 1L;

	public Shape shape() {
		return mShape;
	}
	
	public Color color() {
		return mColor;
	}
	
	public void setColor(Color color) {
		mColor = color;
	}
	
	public void setShape(ShapePanel.Shape shape) {
		mShape = shape;
	}
	
	public ShapePanel(Shape shape, Color color, Color backgrColor) {
		mShape = shape;
		mColor = color;
		mBackgroundColor = backgrColor;
	}
	
	@Override
	public Dimension getMinimumSize() {
		Dimension superMinSize = super.getMinimumSize();
		Dimension minimumSize = new Dimension(Math.min(superMinSize.width,  100), Math.min(superMinSize.height,  100));
		return minimumSize;
	}
	
	private void drawPolygon(int n, int x, int y, int r, Graphics g) {
		int[] xPoints = new int[n];
		int[] yPoints = new int[n];
		
		for (int i = 0; i < n; i++) {
			
			xPoints[i] = (int)(x + r * Math.cos(2 * Math.PI * i / n));
			yPoints[i] = (int)(y + r * Math.sin(2 * Math.PI * i / n));
		}
		g.fillPolygon(xPoints, yPoints, n);
	}
	
	public void paintComponent(Graphics g) {
		
		int w = this.getWidth();
		int xOffset = (int)(0.05 * w);
		int h = this.getHeight();
		
		g.setColor(mBackgroundColor);
		g.fillRect(0, 0, w, h);

		int shapeWidth = (int)(w * 0.9);
		int shapeHeight = (int)(h * 0.9);
		int yOffset = (int)(0.05 * h);
		int d = Math.min(shapeWidth, shapeHeight);
		
		g.setColor(mColor);
		switch(mShape) {
		case Square:
			g.fillRect(xOffset, yOffset, shapeWidth, shapeHeight);
			break;
		case Triangle:
//			this is very simple polygon, so we don't use common drawPolygon routine
			int[] xPoints = {xOffset, xOffset + shapeWidth, xOffset + shapeWidth/2 };
			int[] yPoints = {yOffset, yOffset, yOffset + shapeHeight };
			g.fillPolygon(xPoints, yPoints, 3);
			break;
		case Circle:
			g.fillOval(xOffset, yOffset, d, d);
			break;
		case Hexagon:
			drawPolygon(6, xOffset + shapeWidth/2, yOffset + shapeHeight/2, d/2, g);
			break;
		case Pentagon:
			drawPolygon(5, xOffset + shapeWidth/2, yOffset + shapeHeight/2, d/2, g);
		default:
			//Pentagon
		}
	}
}
